import axios from "axios";

import { createSlice } from "@reduxjs/toolkit";

const billStore = createSlice({
    name:"billStore",
    initialState:{
        billList:[]
    },
    reducers:{
        //setBillList
        setBillList(state,active){
            state.billList=active.payload
        },
        addBill(state,active){
            state.billList.push(active.payload)
        },
    }
})
///异步处理数据
function getBillListSync(){
    return async(dispath)=>{
        //请求数据
        const res=await axios.get("http://localhost:4007/api1")
        console.log(res);
        dispath(setBillList(res.data))
    }
}
function addBillListSync(data){
    return async(dispath)=>{
        //请求数据
        const res=await axios.post("http://localhost:4007/api1",data)
        console.log(res);
        dispath(addBill(res.data))
    }
}
export {getBillListSync,addBillListSync}

export const {setBillList,addBill}=billStore.actions

export default billStore.reducer